package com.wb.server.tcp.server.handler;

import com.wb.server.processor.DataProcessorFactory;
import com.wb.tcp.common.utils.HexConvert;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @author cy
 * @date 2022/4/15
 **/
@Slf4j
public class ServerHandler extends SimpleChannelInboundHandler<ByteBuf> {

    private DataProcessorFactory dataProcessorFactory;

    public ServerHandler(DataProcessorFactory dataProcessorFactory) {
        this.dataProcessorFactory = dataProcessorFactory;
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf byteBuf) throws Exception {
        int len = byteBuf.readableBytes();
        byte[] bytes = new byte[len];
        byteBuf.readBytes(bytes);
        String hexString = HexConvert.BinaryToHexString(bytes);
        dataProcessorFactory.processor(hexString);
    }
}
